package com.spi.controller;


import com.alibaba.fastjson.JSON;
import com.spi.model.User;
import com.spi.mqtrans.MqTransactionListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate ;

    @Autowired
    private MqTransactionListener mqTransactionListener ;

    @RequestMapping("/user01")
    public void user01(){
        System.out.println("进入到user服务 user01方法。。");
    }

    @RequestMapping("/user02")
    public void user02(){
        System.out.println("准备发送消息survey-topic1");
        Map<String,String> map = new HashMap<>();
        map.put("content","生产者发送消息。。survey-topic1.");
        //获取时间 年月日时分秒
        LocalDateTime now = LocalDateTime.now();
        String year = String.valueOf(now.getYear());
        String month = String.valueOf(now.getMonthValue());
        String day = String.valueOf(now.getDayOfMonth());
        String hour = String.valueOf(now.getHour());
        String minute = String.valueOf(now.getMinute());
        String second = String.valueOf(now.getSecond());
        String date = year + month + day + hour + minute + second;
        map.put("msgid",date);
        Message<Map<String,String>> build = MessageBuilder.withPayload(map).build();
        rocketMQTemplate.asyncSend("survey-topic1", build, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                String msgId = sendResult.getMsgId();
                System.out.println("survey-topic1消息投递成功。。消息id为：" + msgId);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("survey-topic1消息投递失败。。");
            }
        });
    }

    @RequestMapping("/user03")
    public void userStatusChange(String userid){
        User user = new User();
        user.setUserid(userid);
        user.setStatus(2);
        String msg = JSON.toJSONString(user);
        Map<String,String> map = new HashMap<>();
        map.put("user",msg);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(date);
        String substring = userid.substring(5,userid.length());
        String messageid = format + substring ;
        map.put("messageid", messageid);
        String jsonString = JSON.toJSONString(map);
        Message<String> build = MessageBuilder.withPayload(jsonString).build();
        //Message<String> build = Message.withPayload(msg).setHeader("proc_id", UUID.randomUUID())build();
        ((TransactionMQProducer) rocketMQTemplate.getProducer()).setTransactionListener(mqTransactionListener);
        log.info("producer准备发送数据。。");
        TransactionSendResult transactionSendResult = rocketMQTemplate.sendMessageInTransaction("survey-topic2", build, null);
        log.info("事务消息结果：{}", transactionSendResult);
    }
}
